package com.program.facesigninsystem.controller;

import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.program.facesigninsystem.entity.AdminGroup;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.service.IAdminGroupService;
import com.program.facesigninsystem.util.PageUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author 盛旭雯
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/admin")
public class AdminGroupController {
    private static Logger log = Logger.getLogger(AdminGroupController.class.toString());
    @Autowired
    public IAdminGroupService iAdminGroupService;
    @RequestMapping(value = "/join",method = RequestMethod.GET)
    @CurrentLimiter(QPS=5)
    public ResponseModel manageUser(@RequestParam("uid")String uid,@RequestParam("bindid")int bindid,@RequestParam("groupname")String groupname){
        AdminGroup adminGroup =iAdminGroupService.selectExist(uid,bindid);
        if(adminGroup==null){
            try{
                iAdminGroupService.insertAdminGroup(uid, bindid,groupname);
                return new ResponseModel(200,"添加成功");
            }catch (Exception e){
                return new ResponseModel(198,"参数错误");
            }
        }else{
            return new ResponseModel(199,"该用户已被添加");
        }
    }

    @RequestMapping(value = "/deleAdminGroup",method = RequestMethod.GET)
    public  ResponseModel deleteAdminGroup(@RequestParam("uid")String uid,@RequestParam("bindid")int bindid){
        try {
            iAdminGroupService.deleteAdminGroup(uid, bindid);
            return new ResponseModel(200,"删除成功");
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
            return new ResponseModel(113,"删除失败");
        }
    }

    @RequestMapping(value = "/selectList",method = RequestMethod.GET)
    public void selectList(HttpServletResponse resp, @RequestParam(value = "uid")String uid,
                           @RequestParam(value = "pageNum", required = false ,defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", required = false ,defaultValue = "10") int pageSize){
        try {
            List<Map<String,Object>> mapList=iAdminGroupService.selectList(uid);
            try {
                JSONObject jsonObject = PageUtil.wrap2(mapList,"member",pageSize,pageNum);
                resp.setCharacterEncoding("utf-8");
                /*得到输出流*/
                PrintWriter respWriter = resp.getWriter();
                /*将JSON格式的对象toString()后发送*/
                respWriter.append(jsonObject.toString());
            } catch (Exception e) {
                log.log(Level.SEVERE,"Error parsing date",e);
            }
        }catch (Exception e){
            log.log(Level.SEVERE,"Error parsing date",e);
        }
    }
}
